package org.stimuliband.dsp.dft;

import java.util.ArrayList;
import java.util.List;

public class DFT {

    final int signal[];

    public DFT(int[] signal) {
        this.signal = signal;
    }

    void compute() {
        double[] reSignal = new double[signal.length];
        double[] imSignal = new double[signal.length];
        double[] mgSignal = new double[signal.length];
        for (int k = 0; k < signal.length / 2 - 1; k++) {
            reSignal[k] = 0;
            imSignal[k] = 0;
            for (int n = 0; n < signal.length - 1; n++) {
                reSignal[k] += signal[n] * Math.cos(2 * Math.PI * n * k / signal.length);
                imSignal[k] += signal[n] * Math.sin(2 * Math.PI * n * k / signal.length);
            }

            // normalization
            reSignal[k] = reSignal[k] * 2 / signal.length;
            imSignal[k] = imSignal[k] * -2 / signal.length;

            mgSignal[k] = Math.sqrt(reSignal[k] * reSignal[k] + imSignal[k] * imSignal[k]);
        }

        double max[] = new double[10];
        int top[] = new int[10];

        List<Integer> used = new ArrayList<Integer>();

        // looking for max pick
        for (int t = 0; t < 10; t++) {
            max[t] = 0;
            top[t] = 0;
            for (int i = 1; i < mgSignal.length / 2; i++) {
                if (mgSignal[i] > max[t]) {
                    if (!used.contains(i)) {
                        max[t] = mgSignal[i];
                        top[t] = i;
                        used.add(i);
                    }
                }
            }
        }


        for (int i = 0; i < 10; i++) {
            System.out.println("Hz=" + top[i] + ", mag=" + max[i]);
        }

    }
}
